<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>java常用API学习 | 牛牛&amp;blog</title><meta name="author" content="牛牛"><meta name="copyright" content="牛牛"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="java常用API学习1、Date日期类:Date类在Java中代表的是系统当前此刻日期时间对象;Date类：        包：java.util.Date。        构造器：            -- public Date():创建当前系统的此刻日期时间对象。            -- public Date(long time):        方法：             -">
<meta property="og:type" content="article">
<meta property="og:title" content="java常用API学习">
<meta property="og:url" content="https://code-ran.gitee.io/2022/11/23/java%E5%B8%B8%E7%94%A8API%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="牛牛&amp;blog">
<meta property="og:description" content="java常用API学习1、Date日期类:Date类在Java中代表的是系统当前此刻日期时间对象;Date类：        包：java.util.Date。        构造器：            -- public Date():创建当前系统的此刻日期时间对象。            -- public Date(long time):        方法：             -">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-11-23T07:28:14.000Z">
<meta property="article:modified_time" content="2023-11-02T03:32:07.138Z">
<meta property="article:author" content="牛牛">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/img/myfavicon.png"><link rel="canonical" href="https://code-ran.gitee.io/2022/11/23/java%E5%B8%B8%E7%94%A8API%E5%AD%A6%E4%B9%A0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'java常用API学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-02 11:32:07'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/style.css"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/ys.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">牛牛&amp;blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">java常用API学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-11-23T07:28:14.000Z" title="发表于 2022-11-23 15:28:14">2022-11-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-02T03:32:07.138Z" title="更新于 2023-11-02 11:32:07">2023-11-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/java%E7%9B%B8%E5%85%B3/">java相关</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="java常用API学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h4 id="java常用API学习"><a href="#java常用API学习" class="headerlink" title="java常用API学习"></a>java常用API学习</h4><h5 id="1、Date日期类"><a href="#1、Date日期类" class="headerlink" title="1、Date日期类:"></a>1、Date日期类:</h5><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Date类在Java中代表的是系统当前此刻日期时间对象;</span><br><span class="line">Date类：</span><br><span class="line">        包：java.util.Date。</span><br><span class="line">        构造器：</span><br><span class="line">            -- <span class="function"><span class="keyword">public</span> <span class="title">Date</span><span class="params">()</span>:创建当前系统的此刻日期时间对象。</span></span><br><span class="line"><span class="function">            -- <span class="keyword">public</span> <span class="title">Date</span><span class="params">(<span class="keyword">long</span> time)</span>:</span></span><br><span class="line"><span class="function">        方法：</span></span><br><span class="line"><span class="function">             -- <span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getTime</span><span class="params">()</span>:返回自 1970 年 1 月 1 日 00:00:00 GMT 以来走过的总的毫秒数。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">        时间记录的两种方式：</span></span><br><span class="line"><span class="function">             a.Date日期对象。</span></span><br><span class="line"><span class="function">             b.时间毫秒值：从1970-01-01 00:00:00开始走到此刻的总的毫秒值。 1s </span>= 1000ms</span><br><span class="line">code example:</span><br><span class="line">        <span class="comment">//创建一个日期对象代表了系统此刻日期时间对象</span></span><br><span class="line">        Date d = <span class="keyword">new</span> Date();</span><br><span class="line">        System.out.println(d);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// b.拿当前日期对象的时间毫秒值</span></span><br><span class="line">        <span class="keyword">long</span> time = d.getTime();</span><br><span class="line">        System.out.println(time);</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">拓展：时间毫秒值的作用。</span><br><span class="line"></span><br><span class="line">时间毫秒值可以用于做时间的计算：例如代码的执行性能分析。</span><br><span class="line">        <span class="comment">// 1.拿到此刻日期时间对象的毫秒值,一般使用System.currentTimeMillis();</span></span><br><span class="line">        <span class="keyword">long</span> startTime = <span class="keyword">new</span> Date().getTime();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; <span class="number">1000000</span> ; i++ )&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;输出：&quot;</span>+i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 2.拿到此刻日期时间对象的毫秒值</span></span><br><span class="line">        <span class="keyword">long</span> endTime = <span class="keyword">new</span> Date().getTime();</span><br><span class="line">        System.out.println( (endTime - startTime) / <span class="number">1000.0</span> +<span class="string">&quot;s&quot;</span>);</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">Date</span><span class="params">()</span>:创建当前系统的此刻日期时间对象。</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">Date</span><span class="params">(<span class="keyword">long</span> time)</span>:把时间毫秒值转换成日期对象。</span></span><br><span class="line"><span class="function">---</span></span><br><span class="line"><span class="function">Date日期对象 -&gt; <span class="title">getTime</span><span class="params">()</span> -&gt; 时间毫秒值</span></span><br><span class="line"><span class="function">时间毫秒值 -&gt; new <span class="title">Date</span><span class="params">(时间毫秒值)</span> -&gt; Date日期对象</span></span><br><span class="line"><span class="function"></span></span><br></pre></td></tr></table></figure>

<h5 id="2、DateFormat时间格式化类"><a href="#2、DateFormat时间格式化类" class="headerlink" title="2、DateFormat时间格式化类:"></a>2、DateFormat时间格式化类:</h5><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">DateFormat作用：</span><br><span class="line">    <span class="number">1.</span>可以把“日期对象”或者“时间毫秒值”格式化成我们喜欢的时间形式。（格式化时间）</span><br><span class="line">    <span class="number">2.</span>可以把字符串的时间形式解析成日期对象。（解析字符串时间）</span><br><span class="line">DateFormat是一个抽象类，不能直接使用，要找它的子类：SimpleDateFormat</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">SimpleDateFormat简单日期格式化类：</span><br><span class="line">    包：java.text.SimpleDateFormat</span><br><span class="line">    构造器：<span class="function"><span class="keyword">public</span> <span class="title">SimpleDateFormat</span><span class="params">(String pattern)</span>：</span></span><br><span class="line"><span class="function">           指定时间的格式创建简单日期格式化对象。</span></span><br><span class="line"><span class="function">    方法：</span></span><br><span class="line"><span class="function">        -- <span class="keyword">public</span> String <span class="title">format</span><span class="params">(Date date)</span>:可以把日期对象格式化成我们喜欢的时间形式，返回的是字符串!</span></span><br><span class="line"><span class="function">        -- <span class="keyword">public</span> String <span class="title">format</span><span class="params">(Object time)</span>:可以把时间毫秒值格式化成我们喜欢的时间形式，返回的是字符串!</span></span><br><span class="line"><span class="function">        -- <span class="keyword">public</span> Date <span class="title">parse</span><span class="params">(String date)</span> <span class="keyword">throws</span> ParseException:把字符串的时间解析成日期对象</span></span><br><span class="line"><span class="function">小结：</span></span><br><span class="line"><span class="function">    简单日期格式化类SimpleDateFormat可以把日期对象格式化成我们喜欢的时间形式</span></span><br><span class="line"><span class="function">        -- <span class="keyword">public</span> String <span class="title">format</span><span class="params">(Date date)</span>:可以把日期对象格式化成我们喜欢的时间形式，返回的是字符串!</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 需求：把此刻日期对象格式化成我们喜欢的形式。</span></span><br><span class="line"><span class="comment">// 1.得到此刻日期对象</span></span><br><span class="line">Date d = <span class="keyword">new</span> Date();</span><br><span class="line">System.out.println(d);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2.创建一个简单日期格式化对象负责格式化日期对象</span></span><br><span class="line"><span class="comment">// 注意：参数是之间的格式。</span></span><br><span class="line">SimpleDateFormat sdf = <span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss EEE a&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 3.开始调用方法格式化时间得到格式化的字符串时间形式</span></span><br><span class="line">String rs = sdf.format(d);</span><br><span class="line">System.out.println(rs);</span><br><span class="line"></span><br><span class="line">----console:</span><br><span class="line">Mon Nov <span class="number">21</span> <span class="number">13</span>:<span class="number">39</span>:<span class="number">45</span> CST <span class="number">2022</span></span><br><span class="line"><span class="number">2022</span>年<span class="number">11</span>月<span class="number">21</span>日 <span class="number">13</span>:<span class="number">39</span>:<span class="number">45</span> 星期一 下午</span><br></pre></td></tr></table></figure>

<h5 id="3、日历类Calendar的使用"><a href="#3、日历类Calendar的使用" class="headerlink" title="3、日历类Calendar的使用"></a>3、日历类Calendar的使用</h5><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Calendar代表了系统此刻日期对应的日历对象。</span><br><span class="line">Calendar是一个抽象类，不能直接创建对象。</span><br><span class="line">Calendar日历类创建日历对象的语法：</span><br><span class="line">    Calendar rightNow = Calendar.getInstance();</span><br><span class="line">Calendar的方法：</span><br><span class="line">    <span class="number">1.</span><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Calendar <span class="title">getInstance</span><span class="params">()</span>: 返回一个日历类的对象。</span></span><br><span class="line"><span class="function">    2.<span class="keyword">public</span> <span class="keyword">int</span> <span class="title">get</span><span class="params">(<span class="keyword">int</span> field)</span>：取日期中的某个字段信息。</span></span><br><span class="line"><span class="function">    3.<span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(<span class="keyword">int</span> field,<span class="keyword">int</span> value)</span>：修改日历的某个字段信息。</span></span><br><span class="line"><span class="function">    4.<span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> field,<span class="keyword">int</span> amount)</span>：为某个字段增加/减少指定的值</span></span><br><span class="line"><span class="function">    5.<span class="keyword">public</span> <span class="keyword">final</span> Date <span class="title">getTime</span><span class="params">()</span>: 拿到此刻日期对象。</span></span><br><span class="line"><span class="function">    6.<span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getTimeInMillis</span><span class="params">()</span>: 拿到此刻时间毫秒值</span></span><br></pre></td></tr></table></figure>

<h5 id="4、Math类的使用"><a href="#4、Math类的使用" class="headerlink" title="4、Math类的使用"></a>4、Math类的使用</h5><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Math用于做数学运算。</span><br><span class="line">Math类中的方法全部是静态方法，直接用类名调用即可。</span><br><span class="line">方法：</span><br><span class="line">      方法名                                        说明                </span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">abs</span><span class="params">(<span class="keyword">int</span> a)</span>                  获取参数a的绝对值：</span></span><br><span class="line"><span class="function">      <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">ceil</span><span class="params">(<span class="keyword">double</span> a)</span>           向上取整</span></span><br><span class="line"><span class="function">      <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">floor</span><span class="params">(<span class="keyword">double</span> a)</span>          向下取整</span></span><br><span class="line"><span class="function">      <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">pow</span><span class="params">(<span class="keyword">double</span> a, <span class="keyword">double</span> b)</span>  获取a的b次幂        </span></span><br><span class="line"><span class="function">      <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">long</span> <span class="title">round</span><span class="params">(<span class="keyword">double</span> a)</span>            四舍五入取整</span></span><br></pre></td></tr></table></figure>

<h5 id="5、BigDecimal大数据类"><a href="#5、BigDecimal大数据类" class="headerlink" title="5、BigDecimal大数据类"></a>5、BigDecimal大数据类</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">浮点型运算的时候直接进行 +  * / 可能会出现数据失真（精度问题）。</span><br><span class="line">BigDecimal可以解决浮点型运算数据失真的问题。</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">BigDicimal类：</span><br><span class="line">    包：java.math.</span><br><span class="line">    创建对象的方式（最好的方式：）</span><br><span class="line">          <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> BigDecimal <span class="title">valueOf</span><span class="params">(<span class="keyword">double</span> val)</span> :包装浮点数成为大数据对象。</span></span><br><span class="line"><span class="function">    方法声明</span></span><br><span class="line"><span class="function">          <span class="keyword">public</span> BigDecimal <span class="title">add</span><span class="params">(BigDecimal value)</span>       加法运算</span></span><br><span class="line"><span class="function">          <span class="keyword">public</span> BigDecimal <span class="title">subtract</span><span class="params">(BigDecimal value)</span>  减法运算 </span></span><br><span class="line"><span class="function">          <span class="keyword">public</span> BigDecimal <span class="title">multiply</span><span class="params">(BigDecimal value)</span>  乘法运算 </span></span><br><span class="line"><span class="function">          <span class="keyword">public</span> BigDecimal <span class="title">divide</span><span class="params">(BigDecimal value)</span>    除法运算</span></span><br><span class="line"><span class="function">          <span class="keyword">public</span> <span class="keyword">double</span> <span class="title">doubleValue</span><span class="params">()</span>:把BigDecimal转换成<span class="keyword">double</span>类型。</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 浮点型运算的时候直接+  * / 可能会出现数据失真（精度问题）。</span></span><br><span class="line">System.out.println(<span class="number">0.1</span> + <span class="number">0.2</span>);</span><br><span class="line">System.out.println(<span class="number">0.09</span> + <span class="number">0.01</span>);</span><br><span class="line">System.out.println(<span class="number">1.0</span>  - <span class="number">0.32</span>);</span><br><span class="line">System.out.println(<span class="number">1.015</span> * <span class="number">100</span>);</span><br><span class="line">System.out.println(<span class="number">1.301</span> / <span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;-------------------------&quot;</span>);</span><br><span class="line"><span class="keyword">double</span> a = <span class="number">0.1</span> ;</span><br><span class="line"><span class="keyword">double</span> b = <span class="number">0.2</span> ;</span><br><span class="line"><span class="keyword">double</span> c = a + b ;</span><br><span class="line">System.out.println(c);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1.把浮点数转换成大数据对象运算</span></span><br><span class="line">BigDecimal a1 = BigDecimal.valueOf(a);</span><br><span class="line">BigDecimal b1 = BigDecimal.valueOf(b);</span><br><span class="line"><span class="comment">//BigDecimal c1 = a1.add(b1);  // 加法</span></span><br><span class="line">BigDecimal c1 = a1.divide(b1); <span class="comment">// 除法</span></span><br><span class="line">System.out.println(c1);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 结果可能需要继续使用!!!</span></span><br><span class="line"><span class="comment">// BigDecimal只是解决精度问题的手段，double数据才是我们的目的！！</span></span><br><span class="line"><span class="keyword">double</span> rs = c1.doubleValue();</span><br><span class="line">System.out.println(rs);</span><br></pre></td></tr></table></figure>

<h5 id="6、包装类的使用"><a href="#6、包装类的使用" class="headerlink" title="6、包装类的使用"></a>6、包装类的使用</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">包装类可以把字符串类型的数值转换成对应的基本数据类型的值（真的很有用）</span><br><span class="line">            -- Xxx.parseXxx(&quot;字符串类型的数值&quot;)</span><br><span class="line">            -- Xxx.valueOf(&quot;字符串类型的数值&quot;):推荐使用!</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//.把字符串类型的数值转换成对应的基本数据类型的值。（真的很有用）</span></span><br><span class="line">String numStr = <span class="string">&quot;23&quot;</span>;</span><br><span class="line"><span class="comment">//int numInt = Integer.parseInt(numStr);</span></span><br><span class="line"><span class="keyword">int</span> numInt = Integer.valueOf(numStr);</span><br><span class="line">System.out.println(numInt+<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">String doubleStr = <span class="string">&quot;99.9&quot;</span>;</span><br><span class="line"><span class="comment">//double doubleDb = Double.parseDouble(doubleStr);</span></span><br><span class="line"><span class="keyword">double</span> doubleDb = Double.valueOf(doubleStr);</span><br><span class="line">System.out.println(doubleDb+<span class="number">0.1</span>);</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">    Java认为一切皆对象。引用数据类型就是对象了。</span><br><span class="line">    但是在Java中<span class="number">8</span>基本数据类型不是对象，只是表示一种数据的类型形式,这<span class="number">8</span>种数据类型显得很突兀。</span><br><span class="line">    Java为了一切皆对象的思想统一，把<span class="number">8</span>种基本数据类型转换成对应的类，这个类称为</span><br><span class="line">    基本数据类型的包装类。</span><br><span class="line"></span><br><span class="line">基本数据类型                    包装类（引用数据类型）</span><br><span class="line">     <span class="function"><span class="keyword">byte</span>                      Byte</span></span><br><span class="line"><span class="function">     <span class="keyword">short</span>                     Short</span></span><br><span class="line"><span class="function">     <span class="keyword">int</span>                       <span class="title">Integer</span><span class="params">(特殊)</span></span></span><br><span class="line"><span class="function">     <span class="keyword">long</span>                      Long</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">     <span class="keyword">float</span>                     Float</span></span><br><span class="line"><span class="function">     <span class="keyword">double</span>                    Double</span></span><br><span class="line"><span class="function">     <span class="keyword">char</span>                      <span class="title">Character</span><span class="params">(特殊)</span></span></span><br><span class="line"><span class="function">     <span class="keyword">boolean</span>                   Boolean</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">自动装箱：可以直接把基本数据类型的值或者变量赋值给包装类。</span></span><br><span class="line"><span class="function">自动拆箱：可以把包装类的变量直接赋值给基本数据类型。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">为什么需要基本数据类型的包装类？</span></span><br><span class="line"><span class="function">（1）Java是面向对象的语言，很多地方需要使用的是对象而不是基本数据类型。例如，List，Map等容器类中基本类型是放不进去的。</span></span><br><span class="line"><span class="function">（2）包装类在原先基本数据类型上，新增加了很多方法，如Integer.<span class="title">valueOf</span><span class="params">(String s)</span></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">包装后的类型和原数据类型有什么区别呢？ </span></span><br><span class="line"><span class="function">拿Int 和Integer来说</span></span><br><span class="line"><span class="function">一、<span class="keyword">int</span> 是基本类型，直接存数值；而integer引用数据类型。</span></span><br><span class="line"><span class="function">二、Int的声明不需要实例化，且变量声明后的初始值为0；Integer的是一个类，初始值为<span class="keyword">null</span>，需要进行实例化，才能对变量数据进行处理。</span></span><br><span class="line"><span class="function">三、Integer类是<span class="keyword">int</span>的包装类，实际开发中Integer被看成一个对象，可以进行数据转换等操作。</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> a = <span class="number">12</span> ;</span><br><span class="line">Integer a1 = <span class="number">12</span> ;  <span class="comment">// 自动装箱</span></span><br><span class="line">Integer a2 = a ;   <span class="comment">// 自动装箱</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">double</span> b = <span class="number">99.9</span>;</span><br><span class="line">Double b1 = <span class="number">99.9</span>; <span class="comment">// 自动装箱</span></span><br><span class="line">Double b2 = b ;   <span class="comment">// 自动装箱</span></span><br><span class="line"></span><br><span class="line">Integer c = <span class="number">100</span> ;</span><br><span class="line"><span class="keyword">int</span> c1 = c ;      <span class="comment">// 自动拆箱</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> d = <span class="number">12</span>;</span><br><span class="line">Integer d1 = <span class="keyword">null</span>; <span class="comment">// 引用数据类型的默认值可以为null</span></span><br><span class="line">Integer d2 = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;-----------------&quot;</span>);</span><br><span class="line">Integer it = Integer.valueOf(<span class="number">12</span>);  <span class="comment">// 手工装箱！</span></span><br><span class="line"><span class="comment">// Integer it1 = new Integer(12); // 手工装箱！</span></span><br><span class="line">Integer it2 = <span class="number">12</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Integer it3 = <span class="number">111</span> ;</span><br><span class="line"><span class="keyword">int</span> it33 = it3.intValue(); <span class="comment">// 手工拆箱</span></span><br><span class="line"><span class="keyword">int</span> it333 = it3;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io">牛牛</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io/2022/11/23/java%E5%B8%B8%E7%94%A8API%E5%AD%A6%E4%B9%A0/">https://code-ran.gitee.io/2022/11/23/java%E5%B8%B8%E7%94%A8API%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://code-ran.gitee.io" target="_blank">牛牛&blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/java/">java</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/11/23/java%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%A4%E7%A9%BA/"><img class="prev-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">java字符串判空</div></div></a></div><div class="next-post pull-right"><a href="/2022/11/23/linux/"><img class="next-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">linux指令收集</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/11/23/Arrays-asList%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Arrays.asList方法"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-23</div><div class="title">Arrays.asList方法</div></div></a></div><div><a href="/2022/11/23/java8%E9%9B%86%E5%90%88%E4%B9%8B%E9%97%B4%E7%9A%84%E6%AF%94%E8%BE%83/" title="java8集合之间的比较"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-23</div><div class="title">java8集合之间的比较</div></div></a></div><div><a href="/2023/06/19/java%E5%88%A4%E6%96%AD%E4%B8%A4%E4%B8%AA%E9%9B%86%E5%90%88%E6%98%AF%E5%90%A6%E7%9B%B8%E7%AD%89/" title="java判断两个集合是否相等"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-19</div><div class="title">java判断两个集合是否相等</div></div></a></div><div><a href="/2023/02/14/java%E4%B8%ADJSONArray%E6%8C%89%E7%85%A7%E6%9F%90%E4%B8%80%E5%B1%9E%E6%80%A7%E6%8E%92%E5%BA%8F/" title="java中JSONArray按照某一属性排序"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-02-14</div><div class="title">java中JSONArray按照某一属性排序</div></div></a></div><div><a href="/2022/11/23/java%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%A4%E7%A9%BA/" title="java字符串判空"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-23</div><div class="title">java字符串判空</div></div></a></div><div><a href="/2022/12/20/java%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E8%BD%AC%E6%8D%A2/" title="java数据类型的转换"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-12-20</div><div class="title">java数据类型的转换</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/ys.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">牛牛</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/code-ran"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本blog内容仅用于个人学习使用!</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#java%E5%B8%B8%E7%94%A8API%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.</span> <span class="toc-text">java常用API学习</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1%E3%80%81Date%E6%97%A5%E6%9C%9F%E7%B1%BB"><span class="toc-number">1.1.</span> <span class="toc-text">1、Date日期类:</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2%E3%80%81DateFormat%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%E5%8C%96%E7%B1%BB"><span class="toc-number">1.2.</span> <span class="toc-text">2、DateFormat时间格式化类:</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3%E3%80%81%E6%97%A5%E5%8E%86%E7%B1%BBCalendar%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.3.</span> <span class="toc-text">3、日历类Calendar的使用</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4%E3%80%81Math%E7%B1%BB%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.4.</span> <span class="toc-text">4、Math类的使用</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5%E3%80%81BigDecimal%E5%A4%A7%E6%95%B0%E6%8D%AE%E7%B1%BB"><span class="toc-number">1.5.</span> <span class="toc-text">5、BigDecimal大数据类</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6%E3%80%81%E5%8C%85%E8%A3%85%E7%B1%BB%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.6.</span> <span class="toc-text">6、包装类的使用</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/springboot%E6%B3%A8%E8%A7%A3%E7%9B%B8%E5%85%B3/" title="springboot注解相关">springboot注解相关</a><time datetime="2024-04-30T03:20:37.000Z" title="发表于 2024-04-30 11:20:37">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/CommandLineRunner%E5%92%8CApplicationRunner%E7%94%A8%E6%B3%95%E4%B8%8E%E5%8C%BA%E5%88%AB/" title="CommandLineRunner和ApplicationRunner用法与区别">CommandLineRunner和ApplicationRunner用法与区别</a><time datetime="2024-04-30T02:42:21.000Z" title="发表于 2024-04-30 10:42:21">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/%E5%A5%BD%E6%96%87%E6%94%B6%E8%97%8F/" title="好文收藏">好文收藏</a><time datetime="2023-12-08T08:40:13.000Z" title="发表于 2023-12-08 16:40:13">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/springboot%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/" title="springboot文件上传下载">springboot文件上传下载</a><time datetime="2023-12-08T03:30:54.000Z" title="发表于 2023-12-08 11:30:54">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/05/linux%E4%BD%BF%E7%94%A8curl%E5%91%BD%E4%BB%A4%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82/" title="linux使用curl命令发送请求">linux使用curl命令发送请求</a><time datetime="2023-12-05T05:56:19.000Z" title="发表于 2023-12-05 13:56:19">2023-12-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2024 By 牛牛</div><div class="footer_custom_text">Hi, Welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">簡</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>